package com.twu.biblioteca;

import java.util.Scanner;

/**
 * Created by lauraionescu on 13/04/2014.
 */
public class MainMenu {

    static final String WELCOME_MESSAGE = "Welcome to the Bangalore Public Library!\n";

    static final String INVALID_OPTION_MESSAGE = "Select a valid option!\n";
    static final String SUCCESSFUL_CHECKOUT = "Thank you! Enjoy the item\n";
    static final String UNSUCCESSFUL_CHECKOUT = "That item is not available.\n";
    static final String SUCCESSFUL_RETURN = "Thank you for returning the item.\n";
    static final String UNSUCCESSFUL_RETURN = "That is not a valid item to return.\n";
    static final String QUIT_MESSAGE = "You have chosen to quit the menu\n";
    static final String LOGIN_MESSAGE = "Welcome to your account!\n";
    static final String UNREGISTERED_MESSAGE = "The username/password combination is not valid\n";
    static final String LOGIN_REQUIRED = "You must be logged in for this option\n";

    private Scanner reader = new Scanner(System.in);
    private Library library = new Library();
    private MenuOptions currentOption;
    private boolean userLoggedIn;

    public MainMenu(){
        currentOption = MenuOptions.LIST_BOOKS;
        userLoggedIn = false;
    }

    public String displayWelcomeMessage(){
        System.out.print(WELCOME_MESSAGE);
        return WELCOME_MESSAGE;
    }

    public String displayMenu(){
        String menuMessage = "You have the following options, please select the preferred option:";
        menuMessage += "\n" + listOptions();

        System.out.println(menuMessage);

        return menuMessage;
    }

    public void open(){
        displayWelcomeMessage();
        displayMenu();
        while (!currentOption.equals(MenuOptions.QUIT)){
            selectOption(reader.nextLine());
        }
    }


    /**
     * Selects the logic for each of the MenuOptions
     * If the option is not recognised, return invalid menu option message
     * @param userInput user input to be converted to valid or invalid menu option
     */
    public String selectOption(String userInput){

        String message;

        try{

            //convert user input to a menu option
            MenuOptions menuOption = MenuOptions.valueOf(userInput.replace(' ','_').trim().toUpperCase());

            switch(menuOption) {

                case LIST_BOOKS:
                    currentOption = MenuOptions.LIST_BOOKS;
                    message = library.displayBookList();
                    break;

                case QUIT:
                    currentOption = MenuOptions.QUIT;
                    message = displayQuitMessage();
                    break;

                case CHECKOUT_BOOK:
                    if (userLoggedIn) {
                        currentOption = MenuOptions.CHECKOUT_BOOK;
                        System.out.println("Please input the title and the author of the book, separated by comma.");
                        message = "Please input the title and the author of the book, separated by comma.";
                        message += library.parseItemToCheckout(reader.nextLine(), true);
                    } else {
                        message = LOGIN_REQUIRED;
                        System.out.print(message);
                    }
                    break;

                case RETURN_BOOK:
                    if (userLoggedIn) {
                        currentOption = MenuOptions.RETURN_BOOK;
                        System.out.println("Please input the title and the author of the book, separated by comma.");
                        message = "Please input the title and the author of the book, separated by comma.";
                        message += library.parseItemToCheckin(reader.nextLine(), true);
                    }else {
                        message = LOGIN_REQUIRED;
                        System.out.print(message);
                    }
                    break;

                case LIST_MOVIES:
                    currentOption = MenuOptions.LIST_MOVIES;
                    message = library.displayMovieList();
                    break;

                case CHECKOUT_MOVIE:
                    if (userLoggedIn) {
                        currentOption = MenuOptions.CHECKOUT_MOVIE;
                        System.out.println("Please input the title, year, and the director of the film, separated by commas.");
                        message = "Please input the title, year, and the director of the film, separated by commas.";
                        message += library.parseItemToCheckout(reader.nextLine(), false);
                    }else {
                        message = LOGIN_REQUIRED;
                        System.out.print(message);
                    }
                    break;

                case RETURN_MOVIE:
                    if (userLoggedIn) {
                        currentOption = MenuOptions.RETURN_MOVIE;
                        System.out.println("Please input the title and the author of the film, separated by comma.");
                        message = "Please input the title and the author of the book, separated by comma.";
                        message += library.parseItemToCheckin(reader.nextLine(), false);
                    }else {
                        message = LOGIN_REQUIRED;
                        System.out.print(message);
                    }
                    break;


                case LOGIN:
                    currentOption = MenuOptions.LOGIN;
                    if (!userLoggedIn) {
                        System.out.println("Username:");
                        String username = reader.nextLine();
                        System.out.println("Password:");
                        String password = reader.nextLine();
                        User user = new User(username, password, "N/A", "N/A", "N/A");
                        message = login(user);
                        System.out.println(message);
                    } else {
                        message = displayInvalidOption();
                    }
                    break;

                case DISPLAY_USER_INFO:
                    currentOption = MenuOptions.DISPLAY_USER_INFO;
                    if (userLoggedIn){
                        message = displayUserInfo();
                    } else {
                        message = displayInvalidOption();
                    }
                    break;

                default:
                    currentOption = MenuOptions.INVALID_OPTION;
                    message = displayInvalidOption();
                    break;
            }
        } catch (Exception exception){
            currentOption = MenuOptions.INVALID_OPTION;
            message = displayInvalidOption();
        }

        return message;

    }


    public String listOptions(){

        String options = "";

        int i = 1;
        for (MenuOptions option : MenuOptions.values()) {
            if (userLoggedIn){
                if (!option.equals(MenuOptions.INVALID_OPTION) && !option.equals(MenuOptions.LOGIN)) {
                        options += option.toString() + "\n";
                        i++;
                }
            } else {
                if (!option.equals(MenuOptions.INVALID_OPTION) && !option.equals(MenuOptions.DISPLAY_USER_INFO)) {
                    options += option.toString() + "\n";
                    i++;
                }
            }

        }
        return options;
    }

    public String displayInvalidOption(){
        System.out.print(INVALID_OPTION_MESSAGE);
        return INVALID_OPTION_MESSAGE;
    }

    public String displayQuitMessage(){
        System.out.print(QUIT_MESSAGE);
        return QUIT_MESSAGE;
    }

    public String login(User registeredUser) {
        if (library.login(registeredUser)) {
            userLoggedIn = true;
            displayMenu();
            return LOGIN_MESSAGE;
        } else {
            return UNREGISTERED_MESSAGE;
        }
    }

    public String displayUserInfo(){
        System.out.println(library.getCurrentUser().getUserDetails());
        return library.getCurrentUser().getUserDetails();
    }
}
